package com.bf.electroplating.controller;

import com.bf.electroplating.pojo.entity.ComWorkshop;
import com.bf.electroplating.service.ComWorkshopService;
import com.bf.electroplating.service.impl.WorkshopDataMigrationServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 车间数据迁移控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/workshop/migration")
@Tag(name = "车间数据迁移", description = "车间数据迁移相关接口")
public class WorkshopDataMigrationController {

    @Autowired
    private WorkshopDataMigrationServiceImpl workshopDataMigrationService;

    @Autowired
    private ComWorkshopService comWorkshopService;

    @PostMapping("/all")
    @Operation(summary = "迁移所有车间数据", description = "迁移所有车间数据及其附件到新的MinIO")
    public Map<String, Object> migrateAllWorkshopData() {
        Map<String, Object> result = new HashMap<>();
        try {
            workshopDataMigrationService.migrateAllWorkshopData();
            result.put("success", true);
            result.put("message", "所有车间数据迁移完成");
        } catch (Exception e) {
            log.error("迁移所有车间数据失败", e);
            result.put("success", false);
            result.put("message", "迁移失败：" + e.getMessage());
        }
        return result;
    }

    @PostMapping("/workshop/{workshopId}")
    @Operation(summary = "迁移指定车间数据", description = "迁移指定车间数据及其附件到新的MinIO")
    public Map<String, Object> migrateWorkshopData(@PathVariable String workshopId) {
        Map<String, Object> result = new HashMap<>();
        try {
            ComWorkshop workshop = comWorkshopService.getById(workshopId);
            if (workshop == null) {
                result.put("success", false);
                result.put("message", "车间不存在");
                return result;
            }

            workshopDataMigrationService.migrateWorkshopData(workshop);
            result.put("success", true);
            result.put("message", "车间数据迁移完成：" + workshop.getWorkshopName());
        } catch (Exception e) {
            log.error("迁移车间数据失败，车间ID：{}", workshopId, e);
            result.put("success", false);
            result.put("message", "迁移失败：" + e.getMessage());
        }
        return result;
    }

    @GetMapping("/validate/{workshopId}")
    @Operation(summary = "验证迁移结果", description = "验证指定车间的数据迁移结果")
    public Map<String, Object> validateMigrationResult(@PathVariable String workshopId) {
        Map<String, Object> result = new HashMap<>();
        try {
            workshopDataMigrationService.validateMigrationResult(workshopId);
            result.put("success", true);
            result.put("message", "验证完成");
        } catch (Exception e) {
            log.error("验证迁移结果失败，车间ID：{}", workshopId, e);
            result.put("success", false);
            result.put("message", "验证失败：" + e.getMessage());
        }
        return result;
    }
}
